package cn.ztydata.teachingsystem.heaven.dao;

import cn.ztydata.teachingsystem.heaven.entity.exercise.ExerciseBlank;
import com.google.common.collect.Lists;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 习题填空Dao
 * Created by wyh on 2015/2/6.
 */
@Repository
public class ExerciseBlankDao extends BaseDao {
    /**
     * 新增填空
     * @param blank 填空
     * @return int 1/0
     *
     * @author wyh
     * @since  2015-2-5
     */
    public int saveExerciseBlank(ExerciseBlank blank){
        String sql = "INSERT INTO t_exercise_blank(iExerciseId,iAnswerCount,tiHasFormula) VALUES(:exerciseId,:answerCount,:hasFormula)";
        SqlParameterSource sps = new BeanPropertySqlParameterSource(blank);

        KeyHolder key = new GeneratedKeyHolder();
        int result = namedParameterJdbcTemplate.update(sql,sps,key);
        blank.setId(key.getKey().longValue());

        log.debug("新增填空成功");
        return result;
    }

    /**
     * 获取填空集合
     * @param exerciseId
     * @return
     * @author lyj
     * @since 2015-2-9
     */
    public List<ExerciseBlank> getExerciseBlanks(long exerciseId){
        String sql = "SELECT id,iAnswerCount,tiHasFormula FROM t_exercise_blank WHERE iExerciseId = :iExerciseId";

        MapSqlParameterSource param = new MapSqlParameterSource().addValue("iExerciseId", exerciseId);

        //获取填空ID、答案数、是否含公式
        List<ExerciseBlank> exerciseBlanks = namedParameterJdbcTemplate.query(sql, param, new RowMapper<ExerciseBlank>() {
            @Override
            public ExerciseBlank mapRow(ResultSet resultSet, int i) throws SQLException {
                ExerciseBlank exerciseBlank = new ExerciseBlank();

                exerciseBlank.setId(resultSet.getLong("id"));
                exerciseBlank.setAnswerCount(resultSet.getInt("iAnswerCount"));
                exerciseBlank.setHasFormula(resultSet.getInt("tiHasFormula"));

                return exerciseBlank;
            }
        });

        log.debug("获取填空集合");
        return exerciseBlanks;

    }

    /**
     * 获取习题填空数（多题）
     * @param exerciseIds 习题编号集合
     * @return List
     *
     * @author fanruiping
     * @since 2015-03-26
     */
    public List<ExerciseBlank> getExerciseBlanksByIds(final List<Long> exerciseIds) {
        String sql = "SELECT iExerciseId,iAnswerCount FROM t_exercise_blank WHERE iExerciseId IN(:ids)";

        return namedParameterJdbcTemplate.query(sql, new MapSqlParameterSource().addValue("ids", exerciseIds), new RowMapper<ExerciseBlank>() {
            @Override
            public ExerciseBlank mapRow(ResultSet resultSet, int i) throws SQLException {
                ExerciseBlank exerciseBlank = new ExerciseBlank();

                exerciseBlank.setExerciseId(resultSet.getLong("iExerciseId"));
                exerciseBlank.setAnswerCount(resultSet.getInt("iAnswerCount"));

                return exerciseBlank;
            }
        });
    }

    /**
     * 查询填空项及备选答案
     * 
     * @param blankId 填空项ID
     * @return ExerciseBlank
     * 
     * @author cx
     * @since 2015/3/27
     */
    public ExerciseBlank getBlankWithSolutions(final long blankId) {
        String sql = "SELECT eb.*,bs.sTextSolution,bs.sHtmlSolution FROM t_exercise_blank eb JOIN t_blank_solution bs ON eb.id=bs.iBlankId WHERE eb.id=:blankId";

        SqlParameterSource sps = new MapSqlParameterSource().addValue("blankId", blankId);

        final ExerciseBlank blank = new ExerciseBlank();
        final List<String> solutionTexts = Lists.newArrayList();

        blank.setId(blankId);
        blank.setAnswerCount(0);

        namedParameterJdbcTemplate.query(sql, sps, new RowMapper<ExerciseBlank>() {
            @Override
            public ExerciseBlank mapRow(ResultSet resultSet, int i) throws SQLException {
                blank.setAnswerCount(resultSet.getInt("iAnswerCount"));

                solutionTexts.add(resultSet.getString("sTextSolution"));

                return null;
            }
        });

        blank.setSolutionTexts(solutionTexts);

        log.debug("查询填空项及备选答案");
        return blank;
    }
}
